
package org.mule.transport.ftpext;

import java.util.concurrent.Semaphore;
import org.apache.commons.net.ftp.FTPClient;
import org.mule.api.MuleContext;
import org.mule.api.endpoint.ImmutableEndpoint;
import org.mule.transport.ftp.FtpConnector;

/**
 *
 * @author florin
 */
public class FtpExtConnector extends FtpConnector {

    private long fileAge = 0;
    private int maxInboundFtpConnections = 0;
    private int maxOutboundFtpConnections = 0;
    private boolean checkFileAge = false;
    private boolean doNotDelete = false;
    private String moveToPattern = null;
    private String moveToDirectory = null;
    private Semaphore maxInboundFtpConnectionsSemaphore = null;
    private Semaphore maxOutboundFtpConnectionsSemaphore = null;

    public FtpExtConnector(MuleContext context) {
        super(context);
        setConnectionFactoryClass("org.mule.transport.ftpext.FtpExtConnectionFactory");
    }

    @Override
    public String getProtocol() {
        return "ftpext";
    }

    public boolean getCheckFileAge() {

        return checkFileAge;
    }

    /**
     * @return the fileAge
     */
    public long getFileAge() {
        return fileAge;
    }

    /**
     * @param fileAge the fileAge to set
     */
    public void setFileAge(long fileAge) {
        this.fileAge = fileAge;
        checkFileAge = true;
    }

    @Override
    public FTPClient createFtpClient(ImmutableEndpoint endpoint) throws Exception {
        return super.createFtpClient(endpoint);
    }

    /**
     * @return the doNotDelete
     */
    public boolean getDoNotDelete() {
        return doNotDelete;
    }

    /**
     * @param doNotDelete the doNotDelete to set
     */
    public void setDoNotDelete(boolean doNotDelete) {
        this.doNotDelete = doNotDelete;
    }

    /**
     * @return the moveToPattern
     */
    public String getMoveToPattern() {
        return moveToPattern;
    }

    /**
     * @param moveToPattern the moveToPattern to set
     */
    public void setMoveToPattern(String moveToPattern) {
        this.moveToPattern = moveToPattern;
    }

    /**
     * @return the moveToDirectory
     */
    public String getMoveToDirectory() {
        return moveToDirectory;
    }

    /**
     * @param moveToDirectory the moveToDirectory to set
     */
    public void setMoveToDirectory(String moveToDirectory) {
        this.moveToDirectory = moveToDirectory;
    }

    /**
     * @return the maxFtpConnections
     */
    public int getMaxInboundFtpConnections() {
        return maxInboundFtpConnections;
    }

    /**
     * @param maxFtpConnections the maxFtpConnections to set
     */
    public void setMaxInboundFtpConnections(int maxInboundFtpConnections) {
        this.maxInboundFtpConnections = maxInboundFtpConnections;
        maxInboundFtpConnectionsSemaphore = new Semaphore(maxInboundFtpConnections);
    }

    public void acquireInboundFtpConnectionPermit() throws InterruptedException {
        if (maxInboundFtpConnections > 0) {
            maxInboundFtpConnectionsSemaphore.acquire();
        }

    }

    public void releaseInboundFtpConnectionPermit() {
        if (maxInboundFtpConnections > 0) {
            maxInboundFtpConnectionsSemaphore.release();
        }

    }

    /**
     * @return the maxOutboundFtpConnections
     */
    public int getMaxOutboundFtpConnections() {
        return maxOutboundFtpConnections;
    }

    /**
     * @param maxOutboundFtpConnections the maxOutboundFtpConnections to set
     */
    public void setMaxOutboundFtpConnections(int maxOutboundFtpConnections) {
        this.maxOutboundFtpConnections = maxOutboundFtpConnections;
        maxOutboundFtpConnectionsSemaphore = new Semaphore(maxOutboundFtpConnections);
    }

    public void acquireOutoundFtpConnectionPermit() throws InterruptedException {
        if (maxOutboundFtpConnections > 0) {
            maxOutboundFtpConnectionsSemaphore.acquire();
        }

    }

    public void releaseOutoundFtpConnectionPermit() {
        if (maxOutboundFtpConnections > 0) {
            maxOutboundFtpConnectionsSemaphore.release();
        }

    }
}
